﻿using FileData.Currency;
using FileData.Currency.EditHelper;
using FileData.Utils.Currency;
using FileData.Utils.Currency.FileUtils;
using FileData.Utils.Helper;
using System;
using System.Collections.Generic;

namespace FileData.DataUtils.ViewUtils
{
    internal class DataTool
    {
        /// <summary>
        /// 批处理 处理文件夹下所有文件
        /// </summary>
        /// <param name="RepRlue"></param>
        /// <param name="Repto"></param>
        /// <param name="FloderPath"></param>
        public void RepFileContentWithFloder(string RepRlue, string Repto, string FloderPath)
        {
            int CompltedCount = 0;
            List<string> FileList = new FloderNameHelper().GetFullFileNameList(FloderPath);
            foreach (string SingleFile in FileList)
            {
                new PrintLog().PercentLog(FileList.Count, ++CompltedCount);
                RepFileContent(RepRlue, Repto, SingleFile);
            }
        }

        /// <summary>
        /// 添加正则规则
        /// </summary>
        /// <param name="Rule">规则</param>
        /// <param name="RepTo">替换为</param>
        /// <param name="FileName"></param>
        public void AddRule(string Rule, string RepTo, string FileName)
        {
            string FloderPath = StaticValue.BinPath + "Rule\\";
            new FloderHelper().FloderExits(FloderPath, true);
            string SaveFilePath = string.Format("{0}{1}.txt", FloderPath, FileName);
            string ModeText = "\r\n<rinfo><rep>{0}</rep><to>{1}</to></rinfo>";
            FileHelper.AppendUTF8Text(SaveFilePath, string.Format(ModeText, new TextHelper().UrlEncodeToUtf8(Rule), new TextHelper().UrlEncodeToUtf8(RepTo)));
            Console.WriteLine(SaveFilePath);
        }

        /// <summary>
        /// 用本地正则文件批量清理文件内容
        /// </summary>
        /// <param name="RegFilePath"></param>
        /// <param name="FloerPath"></param>
        public void BatchRegFloder(string RegFilePath, string FloerPath)
        {
            foreach (var item in new FloderNameHelper().GetFullFileNameList(FloerPath))
            {
                Console.WriteLine(new FileNameHelper().GetFileNameWithoutExtension(item));
                BatchRegFile(RegFilePath, item);
            }
        }

        /// <summary>
        /// 用本地正则文件批量清理文件内容
        /// </summary>
        /// <param name="RegFilePath"></param>
        /// <param name="FilePath"></param>
        public void BatchRegFile(string RegFilePath, string FilePath)
        {
            BatchRegFile(RegFilePath, FilePath, new FileNameHelper().CreateFileNameWithChlidFloder(FilePath, "正则清理"));
        }

        public void BatchRegFile(string RegFilePath, string FilePath, string NewFilePath)
        {
            List<RegInfo> RegList = new List<RegInfo>();
            foreach (var item in HtmlHelper.GetHtmlValueList(FileHelper.ReadAllText(RegFilePath), "//rinfo"))
            {
                RegList.Add(new RegInfo
                {
                    Reg1 =
                    new TextHelper().UrlDecodeToUtf8(new HtmlHelper().GetFirstText(item, "//rep"))
                    ,
                    RegTo1 = new TextHelper().UrlDecodeToUtf8(new HtmlHelper().GetFirstText(item, "//to"))
                });
            }
            BatchRegFile(RegList, FilePath, NewFilePath);
        }

        /// <summary>
        /// 用本地正则文件批量清理文件内容
        /// </summary>
        /// <param name="RegList"></param>
        /// <param name="FilePath"></param>
        /// <param name="NewFilePath"></param>
        private void BatchRegFile(List<RegInfo> RegList, string FilePath, string NewFilePath)
        {
            string Content = FileHelper.ReadAllText(FilePath);
            foreach (var item in RegList)
            {
                Content = TextHelper.ReplaceText(Content, item.Reg1, item.RegTo1);
            }
            FileHelper.WriteUTF8Text(NewFilePath, Content);
        }

        /// <summary>
        /// 正则结构体
        /// </summary>
        private struct RegInfo
        {
            private string Reg;
            private string RegTo;

            public string Reg1 { get => Reg; set => Reg = value; }
            public string RegTo1 { get => RegTo; set => RegTo = value; }
        }

        /// <summary>
        ///  替换单个文件
        /// </summary>
        /// <param name="RepRlue"></param>
        /// <param name="Repto"></param>
        /// <param name="FilePath"></param>
        public void RepFileContent(string RepRlue, string Repto, string FilePath)
        {
            string Contnt = FileHelper.ReadAllText(FilePath);
            Contnt = TextHelper.ReplaceText(Contnt, RepRlue, Repto);
            string NewFileName = new FileNameHelper().CreateFileNameWithChlidFloder(FilePath, "已处理");

            FileHelper.WriteUTF8Text(NewFileName, Contnt);
        }

        /// <summary>
        /// 批量解码并修复文件名中含有的HTML转义字符
        /// </summary>
        /// <param name="FloderPath"></param>
        public void RestoreFloder(string FloderPath)
        {
            int CompltedCount = 0;
            List<string> FileList = new FloderNameHelper().GetFullFileNameList(FloderPath);
            foreach (string SingleFile in FileList)
            {
                new PrintLog().PercentLog(FileList.Count, ++CompltedCount);
                RestoreFileName(SingleFile);
            }
        }

        /// <summary>
        /// 解码并修复文件名中含有的HTML转义字符
        /// </summary>
        /// <param name="FilePath"></param>
        public void RestoreFileName(string FilePath)
        {
            string FileName =
                TextHelper.ReplaceBadChar(
                new TextHelper().HtmlDecodeToUtf8(

                new FileNameHelper().GetFileName(FilePath)
                )
                );
            string FlodePath = new FloderNameHelper().GetFloderPath(FilePath);
            Console.WriteLine(FlodePath + FileName);
            new FileHelper().Rename(FilePath, FlodePath + FileName);
        }

        /// <summary>
        /// 根据正则替换修正文件名
        /// </summary>
        /// <param name="FloderPath"></param>
        /// <param name="Pattern"></param>
        /// <param name="NewText"></param>
        public void RepFileNameWithFloder(string FloderPath, List<string> Pattern, string NewText)
        {
            int CompltedCount = 0;
            List<string> FileList = new FloderNameHelper().GetFullFileNameList(FloderPath);
            foreach (string SingleFile in FileList)
            {
                new PrintLog().PercentLog(FileList.Count, ++CompltedCount);
                RepFileName(SingleFile, Pattern, NewText);
            }
        }

        /// <summary>
        /// 根据正则替换修正文件名
        /// </summary>
        /// <param name="FilePath"></param>
        public void RepFileName(string FilePath, List<string> PatternList, string NewText)
        {
            foreach (string Pattern in PatternList)
            {
                if (RepFileName(FilePath, Pattern, NewText))
                    break;
            }
        }

        /// <summary>
        /// 根据正则替换修正文件名 成功返回true
        /// </summary>
        /// <param name="FilePath"></param>
        public bool RepFileName(string FilePath, string Pattern, string NewText)
        {
            Console.WriteLine(FilePath);
            string FileName = new FileNameHelper().GetFileName(FilePath);

            string NewFileName = TextHelper.ReplaceBadChar(
                 TextHelper.ReplaceText(FileName, Pattern, NewText)
                );

            if (FileName == NewFileName)
            {
                return false;
            }

            string FlodePath = new FloderNameHelper().GetFloderPath(FilePath);
            new FileHelper().Rename(FilePath, FlodePath + NewFileName);
            return true;
        }

        /// <summary>
        /// 批量根据正则替换修正文件名
        /// </summary>
        /// <param name="FloderPath"></param>
        public void RepFileNameWithFloder(string FloderPath, string Pattern, string NewText)
        {
            int CompltedCount = 0;
            List<string> FileList = new FloderNameHelper().GetFullFileNameList(FloderPath);
            foreach (string SingleFile in FileList)
            {
                new PrintLog().PercentLog(FileList.Count, ++CompltedCount);
                RepFileName(SingleFile, Pattern, NewText);
            }
        }

        /// <summary>
        /// 批量给文件追加文本
        /// </summary>
        /// <param name="FloderPath">文件夹路径</param>
        /// <param name="CustomContent">自定义文本</param>
        public void AddContentWithFloder(string FloderPath, string CustomContent = "")
        {
            int CompltedCount = 0;
            List<string> FileList = new FloderNameHelper().GetFullFileNameList(FloderPath);
            foreach (string SingleFile in FileList)
            {
                new PrintLog().PercentLog(FileList.Count, ++CompltedCount);
                AddContentWithFile(SingleFile, new DataTool().CreateVirtualCloudDirverContent(CustomContent));
            }
        }

        /// <summary>
        /// 给单个文件追加文本
        /// </summary>
        /// <param name="FilePath"></param>
        /// <param name="AddContent"></param>
        public void AddContentWithFile(string FilePath, string AddContent)
        {
            string Content = FileHelper.ReadAllText(FilePath);
            Content = Content + AddContent;
            string NewFilePath = new FileNameHelper().CreateFileNameWithChlidFloder(FilePath, "追加文本");
            FileHelper.WriteUTF8Text(NewFilePath, Content);
        }

        /// <summary>
        /// 创建虚拟网盘信息
        /// </summary>
        /// <param name="CustomContent">附加在文章尾部的自定义拓展文本</param>
        /// <returns></returns>
        public string CreateVirtualCloudDirverContent(string CustomContent = "")
        {
            string DownloadBox = new HtmlCodeCreate().LableDiv("{0}", "downloadbox");
            string DownloadTip = new HtmlCodeCreate().LableH("下载地址", 2);
            string UrlBox = new HtmlCodeCreate().LableDiv("{0}", "downloadInfo");
            string PanLink = "https://pan.baidu.com/s/" + new TextHelper().GetRandomString(15);
            string PanInfo = new HtmlCodeCreate().LableA("下载地址", "baidu", PanLink);

            string PwdBox = new HtmlCodeCreate().LableP(string.Format("百度网盘：{0}", PanInfo), "url");
            string ExtPwd = new HtmlCodeCreate().LableP(string.Format("提取码：{0}", new TextHelper().GetRandomString(4).ToLower()), "extPwd");
            //创建百度网盘盒子
            UrlBox = string.Format(UrlBox, PwdBox + ExtPwd);
            DownloadBox = string.Format(DownloadBox, DownloadTip + UrlBox);
            DownloadBox += CustomContent;
            return DownloadBox;
        }
    }
}